function y=distance(x)
    
% variables to be used in 'distance.m' and 'evaluate.m'
    global rho gma delta1 delta2 beta T N ...
        psi_m psi_f alpha ...
        level_model level_data share_model share_data ...
        ind11 ind12 ind21 ind22 ...
        Cobb_Douglas price_reduction decomposition ...
        a_m a_f a_g a_y a_h ...
        W W_m W_f p P_c ...
        age married insample insample_data nkid X_theta phi_theta ...
        param_W_m param_W_f ...
        R T_D T_R expr_m0 expr_f0 W_m_base W_f_base
    
    x=exp(x);

    param_alpha=zeros(2,2);
    param_psi_m=zeros(2,2);
    param_psi_f=zeros(2,2);

    param_alpha(1,1:2)=x(1:2);    
    param_psi_m(1,1:2)=x(3:4);

    param_alpha(2,1:2)=x(5:6);    
    param_psi_m(2,1:2)=x(7:8);
    param_psi_f(2,1:2)=x(9:10);

    for m=1:2
        for e=1:2

            eval(['alpha(ind' int2str(m) int2str(e) ')=param_alpha(m,e);']);        
            eval(['psi_m(ind' int2str(m) int2str(e) ')=param_psi_m(m,e);']);

            if (m==2)
                eval(['psi_f(ind' int2str(m) int2str(e) ')=param_psi_f(m,e);']);        
            else
                eval(['psi_f(ind' int2str(m) int2str(e) ')=0;']);
            end
        end
    end

    evaluate;

    y=0;

    for m=1:2
        for e=1:2
            for j=1:3
                if (m~=1 | j~=3)
                    y=y+(level_model(m,e,j)/level_data(m,e,j)-1)^2;
                end
            end
        end
    end
end